// 打印工具函数
export class PrintUtils {
  static HIDDEN_CONTAINER_ID = 'hidden-pagination-measure'
  static PAGE_HEIGHT_MM = 297 // A4高度
  static PAGE_WIDTH_MM = 210

  static getPageHeightPx(formatSettings) {
    // 1mm = 3.78px (近似)
    const margin = parseFloat(formatSettings.pageMargin || '2.54') * 2 * 3.78
    return this.PAGE_HEIGHT_MM * 3.78 - margin
  }

  static createHiddenContainer(formatSettings) {
    let container = document.getElementById(this.HIDDEN_CONTAINER_ID)
    if (!container) {
      container = document.createElement('div')
      container.id = this.HIDDEN_CONTAINER_ID
      container.style.position = 'absolute'
      container.style.visibility = 'hidden'
      container.style.left = '-9999px'
      container.style.top = '0'
      container.style.width = `${this.PAGE_WIDTH_MM * 3.78 - parseFloat(formatSettings.pageMargin || '2.54') * 2 * 3.78}px`
      container.style.fontSize = `${formatSettings.fontSize}pt`
      container.style.lineHeight = formatSettings.lineHeight
      container.style.fontFamily = formatSettings.fontFamily
      container.style.padding = '0'
      container.style.boxSizing = 'border-box'
      document.body.appendChild(container)
    }
    return container
  }

  static removeHiddenContainer() {
    const container = document.getElementById(this.HIDDEN_CONTAINER_ID)
    if (container) document.body.removeChild(container)
  }

  static getBottomLineStyle(formatSettings) {
    switch (formatSettings.bottomLine) {
      case 'solid':
        return '1px solid #333'
      case 'dashed':
        return '1px dashed #333'
      default:
        return 'none'
    }
  }

  static createEmptyLine(formatSettings) {
    const lineHeight = parseFloat(formatSettings.fontSize) * parseFloat(formatSettings.lineHeight) * 1.33
    const emptyLineDiv = document.createElement('div')
    emptyLineDiv.className = `record-line ${formatSettings.bottomLine}`
    emptyLineDiv.style.minHeight = `${lineHeight}px`
    emptyLineDiv.style.paddingBottom = '2px'
    // 保证空白行和内容行的 marginBottom 完全一致
    emptyLineDiv.style.marginBottom = formatSettings.lineSpacing || '8mm'
    emptyLineDiv.style.lineHeight = formatSettings.lineHeight
    emptyLineDiv.style.fontSize = `${formatSettings.fontSize}pt`
    emptyLineDiv.style.fontFamily = formatSettings.fontFamily
    emptyLineDiv.style.textIndent = '0'
    emptyLineDiv.style.textAlign = 'left'
    emptyLineDiv.style.borderBottom = this.getBottomLineStyle(formatSettings)
    emptyLineDiv.innerHTML = '&nbsp;'
    return emptyLineDiv
  }

  // 构建头部信息文本
  static buildHeaderInfo(data) {
    if (!data) return ''
    
    // 格式化函数，确保数值字段有合适的默认值
    const formatValue = (value, defaultValue = '    ') => {
      return value ? String(value) : defaultValue
    }
    
    const headerText = `时间${formatValue(data.kssj_y, '    ')}年${formatValue(data.kssj_m, '  ')}月${formatValue(data.kssj_d, '  ')}日${formatValue(data.kssj_h, '  ')}时${formatValue(data.kssj_mi, '  ')}分至${formatValue(data.jssj_y, '    ')}年${formatValue(data.jssj_m, '  ')}月${formatValue(data.jssj_d, '  ')}日${formatValue(data.jssj_h, '  ')}时${formatValue(data.jssj_mi, '  ')}分
地点${formatValue(data.dd, '                    ')}
询问/讯问人${formatValue(data.xwr, '        ')}　工作单位${formatValue(data.xwrgzdw, '                    ')}
记录人${formatValue(data.jlr, '        ')}　工作单位${formatValue(data.jlrgzdw, '                    ')}
被询问/讯问人${formatValue(data.bxwr, '        ')}　性别${formatValue(data.xb, '    ')}　年龄${formatValue(data.nl, '    ')}　出生日期${formatValue(data.crrq, '            ')}
身份证件种类及号码${formatValue(data.zjlx, '        ')}${formatValue(data.zjhm, '                    ')}
现住址${formatValue(data.xzz, '                                                    ')}　联系方式${formatValue(data.lxdh, '                ')}
户籍所在地${formatValue(data.hjzz, '                                                    ')}
（口头传唤/被扭送/自动投案的被询问/讯问人于  月  日  时  分到达，  月  日  时  分离开，本人签名：                  ）
`
    
    return headerText
  }
}
